import logging
from selenium.webdriver.common.by import  By
from selenium.webdriver.chrome import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.remote.webelement import WebElement
from common.uitls.doLog import DoLog

dolog = DoLog()
log = dolog.ste_get_logger(logging.INFO)
class BasePage:
    # 登录以及用户名的元素
    login_unae=(By.XPATH,"//font[@id='ECS_MEMBERZONE']/a[1]")
    # 注册以及推出的元素
    reg_logout=(By.XPATH,"//font[@id='ECS_MEMBERZONE']/a[2]")




    def __init__(self, d=None):
        if d is None:
            self.driver = webdriver.WebDriver()
            self.driver.get('http://192.168.202.136/')
        else:
            # driver接力
            self.driver:WebDriver = d

        self.driver.maximize_window()
        self.driver.implicitly_wait(10)


    def getElement(self,*locator):
        try:
            e: WebElement = WebDriverWait(self.driver, 10, 1).until(lambda x: x.find_element(*locator))

        except Exception as e1:
            log.error(f'发生异常，查找元素{locator},错误元素{e1}')

        else:
            log.info(f"查找元素{locator}成功")
            return e
        finally:
            log.info("查找元素结束")


    def setEleVlue(self, locator, v):
        e = self.getElement(locator)
        e.clear()
        e.send_keys(v)



    def getEleValue(self, locater):

        a=self.getElement(*locater).text
        return a




    def getAlterValue(self):
        alert = self.driver.switch_to.alert
        t = alert.text

        alert.accept()
        return t